package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.entity.ControlDataFreshAirBean;
import com.qiantu.api.entity.DeviceModeValue;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.ui.adapter.DeviceModeValueAdapter;
import com.qiantu.phone.widget.FreshAirSwitchView;
import com.qiantu.phone.widget.WindSpeedSeekBar;

import java.util.List;

import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.EXHAUST_SPEED;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.MODE;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_OFF;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.POWER_ON;
import static com.qiantu.phone.ui.uiutil.LLDeviceUtil.WIND_SPEED;

/**
 * @Author luzx
 * @Date 2022/1/10
 */
public class FreshAirShotView extends BaseDeviceActView {

    private TextView tvFreshAir;
    private FreshAirSwitchView freshAirSwitchView;
    private View windSpeedSeekBarContainer;
    private WindSpeedSeekBar windSpeedSeekBar;
    private View exhaustWindSpeedSeekBarContainer;
    private WindSpeedSeekBar exhaustWindSpeedSeekBar;
    private TextView tvMode;
    private RecyclerView modeValueRecyclerView;
    private DeviceModeValueAdapter mDeviceModeValueAdapter;
    private ControlDataFreshAirBean mAirConditionerInfo;
    private ControlDataFreshAirBean.ControlData controlData;
    private String mode;

    public FreshAirShotView(Context context, String type, String deviceType) {
        super(context, type, deviceType);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fresh_air_shot_view;
    }

    @Override
    public void initView() {
        freshAirSwitchView = findViewById(R.id.fresh_air_switch_view);
        tvFreshAir = findViewById(R.id.tv_fresh_air);
        windSpeedSeekBarContainer = findViewById(R.id.wind_speed_seek_bar_container);
        windSpeedSeekBar = findViewById(R.id.wind_speed_seek_bar);
        exhaustWindSpeedSeekBarContainer = findViewById(R.id.exhaust_wind_speed_seek_bar_container);
        exhaustWindSpeedSeekBar = findViewById(R.id.exhaust_wind_speed_seek_bar);
        tvMode = findViewById(R.id.tv_mode);
        modeValueRecyclerView = findViewById(R.id.mode_value_recycler_view);
//        freshAirSwitchView.setHasAnimation(false);
        freshAirSwitchView.setHasValue(false);
        windSpeedSeekBar.setOnSeekBarChangeListener(new WindSpeedSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int step) {
                String value = getWindSpeedValue(step, false);
                addAct(WIND_SPEED, value);
                freshAirSwitchView.setSpeedName(getWindSpeedName(value, false));
            }
        });
        exhaustWindSpeedSeekBar.setOnSeekBarChangeListener(new WindSpeedSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int step) {
                addAct(EXHAUST_SPEED, getWindSpeedValue(step, true));
            }
        });
        freshAirSwitchView.setOnChangeListener(new FreshAirSwitchView.OnChangeListener() {
            @Override
            public void onSwitch(boolean isOpen) {
                if(isOpen){
                    addAct(POWER, POWER_ON);
//                    String windSpeed = getWindSpeedValue(windSpeedSeekBar.getStep(), false);
//                    addAct(WIND_SPEED, windSpeed);
//                    freshAirSwitchView.setSpeedName(getWindSpeedName(windSpeed, false));
//                    if(exhaustWindSpeedSeekBar.getVisibility() == VISIBLE){
//                        addAct(EXHAUST_SPEED, getWindSpeedValue(exhaustWindSpeedSeekBar.getStep(), true));
//                    }
                }else{
                    addAct(POWER, POWER_OFF);
                    removeAct(MODE);
                    removeAct(WIND_SPEED);
                    removeAct(EXHAUST_SPEED);
                    mode = defaultValue;
                    selectValue(mode);
                }
                windSpeedSeekBar.setOpenState(isOpen);
                exhaustWindSpeedSeekBar.setOpenState(isOpen);
            }
        });
        mAirConditionerInfo = DeviceInfoParseUtil.parseData(getContext(), deviceType, ControlDataFreshAirBean.class);
        controlData = mAirConditionerInfo.getControlData();
        if(controlData != null && controlData.getWindspeed() != null && controlData.getWindspeed().size() > 0){
            windSpeedSeekBar.setVisibility(View.VISIBLE);
            windSpeedSeekBar.setWindSpeedTotalStep(controlData.getWindspeed().size());
        }else{
            tvFreshAir.setVisibility(View.GONE);
            windSpeedSeekBar.setVisibility(View.GONE);
        }
        //排风风速
        if(controlData != null && controlData.getExhaustspeed() != null && controlData.getExhaustspeed().size() > 0){
            tvFreshAir.setVisibility(View.VISIBLE);
            windSpeedSeekBar.setLineHeight(getResources().getDimensionPixelSize(R.dimen.dp_6));
            windSpeedSeekBar.setWindSpeedBarHeight(getResources().getDimensionPixelSize(R.dimen.dp_22));
            windSpeedSeekBar.setWindSpeedBarThumbHeight(getResources().getDimensionPixelSize(R.dimen.dp_36));
            windSpeedSeekBar.setWindSpeedBarOpenCloseThumb(R.attr.icon_devicecontrol_freshair_exhaustair, R.attr.icon_devicecontrol_freshair_exhaustair_close);
            exhaustWindSpeedSeekBarContainer.setVisibility(View.VISIBLE);
            exhaustWindSpeedSeekBar.setVisibility(View.VISIBLE);
            exhaustWindSpeedSeekBar.setWindSpeedTotalStep(controlData.getExhaustspeed().size());
            tvMode.setVisibility(GONE);
        }else{
            tvFreshAir.setVisibility(View.GONE);
            windSpeedSeekBar.setLineHeight(getResources().getDimensionPixelSize(R.dimen.dp_7));
            windSpeedSeekBar.setWindSpeedBarHeight(getResources().getDimensionPixelSize(R.dimen.dp_40));
            windSpeedSeekBar.setWindSpeedBarThumbHeight(getResources().getDimensionPixelSize(R.dimen.dp_40));
            exhaustWindSpeedSeekBarContainer.setVisibility(View.GONE);
            exhaustWindSpeedSeekBar.setVisibility(View.GONE);
            tvMode.setVisibility(VISIBLE);
        }
        if(controlData.getMode() != null && controlData.getMode().size() > 0){
            mDeviceModeValueAdapter = new DeviceModeValueAdapter(getContext());
            mDeviceModeValueAdapter.setData(controlData.getMode());
            mDeviceModeValueAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    if(!freshAirSwitchView.isOpen()){
                        ToastUtils.show(R.string.device_off_disable);
                        return;
                    }
                    mDeviceModeValueAdapter.selectItem(position);
                    DeviceModeValue valueBean = mDeviceModeValueAdapter.getData().get(position);
                    mode = valueBean.getValue() + "";
                    addAct(MODE, mode);
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
            modeValueRecyclerView.setLayoutManager(linearLayoutManager);
            modeValueRecyclerView.setAdapter(mDeviceModeValueAdapter);
        }else{
            tvMode.setVisibility(GONE);
            modeValueRecyclerView.setVisibility(GONE);
            if(exhaustWindSpeedSeekBar.getVisibility() == GONE){
                ConstraintLayout.LayoutParams cl1 = (ConstraintLayout.LayoutParams) freshAirSwitchView.getLayoutParams();
                cl1.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_10);
                ConstraintLayout.LayoutParams cl2 = (ConstraintLayout.LayoutParams) windSpeedSeekBarContainer.getLayoutParams();
                cl2.topMargin = getResources().getDimensionPixelSize(R.dimen.dp_10);
            }
        }
    }

    private void selectValue(String value){
        if(mDeviceModeValueAdapter == null || mDeviceModeValueAdapter.getData() == null || mDeviceModeValueAdapter.getData().size() == 0){
            return;
        }
        boolean hasSame = false;
        for(int i=0; i<mDeviceModeValueAdapter.getData().size(); i++){
            if(mDeviceModeValueAdapter.getData().get(i).getValue() == Integer.valueOf(value)){
                mDeviceModeValueAdapter.selectItem(i);
                hasSame = true;
                break;
            }
        }
        if(!hasSame){
            mDeviceModeValueAdapter.clearSelected();
        }
    }

    @Override
    public void updateView() {
        //默认空的话，添加默认动作开
        if(mDeviceActMap.size() == 0){
            addAct(POWER, POWER_ON);
        }
        //根据已保存动作指令集合更新UI
        boolean isOpen = POWER_ON.equals(getState(POWER, "0"));
        freshAirSwitchView.setOpenState(isOpen);
        mode = getState(MODE, defaultValue);
        String windSpeed = getState(WIND_SPEED, defaultValue);
        windSpeedSeekBar.setStep(getWindSpeedStep(windSpeed, false));
        windSpeedSeekBar.setOpenState(isOpen);
        freshAirSwitchView.setSpeedName(getWindSpeedName(windSpeed, false));
        exhaustWindSpeedSeekBar.setStep(getWindSpeedStep(getState(EXHAUST_SPEED, defaultValue), true));
        exhaustWindSpeedSeekBar.setOpenState(isOpen);
        selectValue(mode);
    }

    private String getWindSpeedName(String value, boolean isExhaust){
        if(value == null){
            return null;
        }
        List<DeviceModeValue> deviceModeValues;
        if(isExhaust){
            deviceModeValues = controlData.getExhaustspeed();
        }else{
            deviceModeValues = controlData.getWindspeed();
        }
        if(deviceModeValues == null || deviceModeValues.size() == 0){
            return null;
        }
        for(int i=0; i<deviceModeValues.size(); i++){
            if(deviceModeValues.get(i).getValue() == Integer.valueOf(value)){
                int resId = getResources().getIdentifier(deviceModeValues.get(i).getName(), "string", getContext().getPackageName());
                return getResources().getString(resId);
            }
        }
        return null;
    }

    private int getWindSpeedStep(String value, boolean isExhaust){
        if(value == null){
            return 0;
        }
        List<DeviceModeValue> deviceModeValues;
        if(isExhaust){
            deviceModeValues = controlData.getExhaustspeed();
        }else{
            deviceModeValues = controlData.getWindspeed();
        }
        if(deviceModeValues == null || deviceModeValues.size() == 0){
            return 0;
        }
        for(int i=0; i<deviceModeValues.size(); i++){
            if(deviceModeValues.get(i).getValue() == Integer.valueOf(value)){
                return i;
            }
        }
        return 0;
    }

    private String getWindSpeedValue(Integer step, boolean isExhaust){
        if(step == null){
            return null;
        }
        List<DeviceModeValue> deviceModeValues;
        if(isExhaust){
            deviceModeValues = controlData.getExhaustspeed();
        }else{
            deviceModeValues = controlData.getWindspeed();
        }
        if(deviceModeValues == null || deviceModeValues.size() == 0){
            return null;
        }
        for(int i=0; i<deviceModeValues.size(); i++){
            if(i == step){
                return deviceModeValues.get(i).getValue() + "";
            }
        }
        return null;
    }
}
